package classfile

/*
变长属性，只存在method_info结构体中
存放字节码等方法相关信息
 */
type CodeAttribute struct {
	cp 				ConstantPool
	MaxStack 		uint16	// 栈的最大深度
	MaxLocals 		uint16	// 局部变量表的大小
	Code 			[]byte	// 字节码
	exceptionTable 	[]*ExceptionTableEntry	// 异常处理表
	attributes 		[]AttributeInfo	// 属性表
}

type ExceptionTableEntry struct {
	startPc uint16
	endPc uint16
	handlerPc uint16
	catchType uint16
}

func (self *CodeAttribute)ReadInfo(reader *ClassReader) {
	self.MaxStack = reader.ReadUint16()
	self.MaxLocals = reader.ReadUint16()
	codeLen := reader.ReadUint32()
	self.Code = reader.ReadBytes(codeLen)
	self.exceptionTable = readExceptionTable(reader)
	self.attributes = readAttributes(reader, self.cp)
}

func readExceptionTable(reader *ClassReader) []*ExceptionTableEntry {
	length := reader.ReadUint16()
	table := make([]*ExceptionTableEntry, length)
	for i := range table {
		table[i] = &ExceptionTableEntry {
			startPc: reader.ReadUint16(),
			endPc: reader.ReadUint16(),
			handlerPc: reader.ReadUint16(),
			catchType: reader.ReadUint16(),
		}
	}
	return table
}